<template>
	<div class="audit-wrapper">
		<!-- 审核确认弹框 -->
		<AuditConfirm ref="auditDialog" :submit="handleAudit" />
		<div class="head">
			<span class="title">
				{{ $t('funds.fund_audit.proxy_rebate_audit_detail') }}
			</span>
			<div v-if="allowAudit" class="right-btn">
				<el-button plain @click="goBack">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button
					v-if="hasPermission('407075')"
					type="success"
					@click="() => $refs.auditDialog.successOpen()"
				>
					{{
						activeName === '1'
							? $t('funds.fund_audit.audit_success_1')
							: $t('funds.fund_audit.audit_success_2')
					}}
				</el-button>
				<el-button
					v-if="hasPermission('407075')"
					type="danger"
					@click="() => $refs.auditDialog.dangerOpen()"
				>
					{{
						activeName === '1'
							? $t('funds.fund_audit.audit_fail_1')
							: $t('funds.fund_audit.audit_fail_2')
					}}
				</el-button>
			</div>
			<div v-else class="right-btn">
				<el-button plain @click="goBack">
					{{ $t('common.back') }}
				</el-button>
			</div>
		</div>
		<div class="audit-content detailBoxStyle flex-row-container">
			<div class="tree-box mr15">
				<CommonProxyTree
					ref="proxyTree"
					:selectedData.sync="selectedData"
					:detailData="detailData"
					:createdAtEnd="proxyCreatedAtEnd"
				/>
			</div>
			<div class="treeRight">
				<ProxyRebate
					v-if="selectedData.type === 1"
					:selectedData="selectedData"
					:detailData="detailData"
				/>
				<RebateBouns
					v-else
					:selectedData="selectedData"
					:detailData="detailData"
				/>
			</div>
		</div>

		<!-- 审核弹窗 -->
		<el-dialog
			:title="$t('funds.fund_audit.audit_dialog_title')"
			center
			:visible.sync="auditDialog.visible"
			width="630px"
			class="audit-dialog"
			@close="closeAuditDialog"
			:close-on-click-modal="false"
		>
			<el-form
				ref="reviwewForm"
				:model="auditDialog"
				:rules="formRules"
				label-width="auto"
			>
				<el-form-item
					:label="$t('common.components.remark')"
					prop="remark"
				>
					<el-input
						v-model="auditDialog.remark"
						clearable
						type="textarea"
						:maxlength="500"
						show-word-limit
						:autosize="{ minRows: 4, maxRows: 4 }"
						v-input="{ name: 'noEmpty' }"
						style="width: 380px"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button
					:disabled="auditDialog.loading"
					@click="auditDialog.visible = false"
				>
					{{ $t('common.cancel') }}
				</el-button>
				<el-button
					type="primary"
					:disabled="auditDialog.loading"
					@click="openSeaMoonDialog(true)"
				>
					<i v-show="auditDialog.loading" class="el-icon-loading"></i>
					<span>{{ $t('common.submit') }}</span>
				</el-button>
			</div>
		</el-dialog>
		<el-dialog
			:visible.sync="rulesDialog.visible"
			:destroy-on-close="true"
			width="1000px"
			:title="`${$t('funds.fund_audit.return_rule')}`"
		>
			<el-table
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				size="mini"
				:data="rulesDialog.tableData"
				:height="400"
				style="width: 100%;"
				:header-cell-style="getRowClass"
				:row-class-name="tableRowClassName"
			>
				<el-table-column
					align="center"
					type="index"
					:label="$t('common.serial_number')"
				></el-table-column>
				<el-table-column
					align="center"
					:label="`${$t('funds.fund_audit.team_water')}`"
					prop="validBetAmount"
				>
					<template slot-scope="scope">
						<span>
							≥ {{ handleCurrency(detailData.currency) }}
							{{
								handleNumber(
									detailData.currency,
									scope.row.validBetAmount
								)
							}}
						</span>
					</template>
				</el-table-column>

				<el-table-column
					align="center"
					:label="$t('funds.fund_audit.active_subordinate')"
					prop="activeUsers"
				>
					<template slot-scope="scope">
						<span>
							≥
							{{
								!!scope.row.activeUsers ||
								scope.row.activeUsers === 0
									? scope.row.activeUsers
									: '-'
							}}
						</span>
					</template>
				</el-table-column>
				<el-table-column
					align="center"
					:label="
						$t('funds.fund_audit.effective_Number_of_active_unit')
					"
					prop="effecactiveUsers"
				>
					<template slot-scope="scope">
						<span>
							≥
							{{
								!!scope.row.effecactiveUsers ||
								scope.row.effecactiveUsers === 0
									? scope.row.effecactiveUsers
									: '-'
							}}
						</span>
					</template>
				</el-table-column>
				<el-table-column
					align="center"
					:label="`${$t('funds.fund_audit.rebate_rate')}`"
					prop="rebateRate"
				>
					<template slot-scope="scope">
						<span>
							{{ handlePercentage(scope.row.rebateRate) }}
						</span>
					</template>
				</el-table-column>
			</el-table>
		</el-dialog>
		<SeaMoonDialog
			:sea-moon-visible.sync="seaMoon.visible"
			:params="seaMoon.params"
			@success="onSeaMoonValidSucceed"
		></SeaMoonDialog>
	</div>
</template>

<script>
import list from '@/mixins/list'
import dayjs from 'dayjs'
import SeaMoonDialog from '@/components/SeaMoon'
import CommonProxyTree from '@/components/CommonProxyTree/index'
import RebateBouns from '@/components/Funds/RebateBouns'
import ProxyRebate from '@/components/Funds/ProxyRebate'
import AuditConfirm from '@/components/AuditConfirm'

export default {
	components: {
		SeaMoonDialog,
		CommonProxyTree,
		RebateBouns,
		ProxyRebate,
		AuditConfirm
	},
	mixins: [list],
	props: {
		allowAudit: {
			type: Boolean,
			required: true
		},
		activeName: {
			type: String,
			required: true
		},
		detailData: {
			type: Object,
			required: true
		}
	},
	data() {
		return {
			tableLoading: false,
			selectedData: {
				type: 1,
				userName: ''
			},
			// 活跃弹窗
			activeNumDialog: {
				rowData: {},
				loading: false,
				visible: false,
				title: '活跃人数',
				type: 1, // 1:active  2:validActive
				tableData: [],
				page: {
					total: 0,
					loading: false,
					pageNum: 1,
					pageSize: 100
				}
			},
			// 规则弹窗
			rulesDialog: {
				visible: false,
				level: undefined,
				tableData: []
			},
			// 审核弹窗
			auditDialog: {
				loading: false,
				visible: false,
				result: undefined,
				remark: undefined
			},
			seaMoon: {
				isAudit: false,
				visible: false,
				params: {}
			}
		}
	},
	computed: {
		formRules() {
			return {
				remark: [
					{
						required: true,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					},
					{
						min: 2,
						max: 500,
						message: this.$t('funds.fund_audit.form_rules_tip2'),
						trigger: 'blur'
					}
				]
			}
		},
		proxyCreatedAtEnd() {
			return dayjs(String(this.detailData?.cycleEndDate)).format(
				'YYYY-MM-DD 23:59:59'
			)
		}
	},
	watch: {},
	created() {},
	methods: {
		onSeaMoonValidSucceed(data) {
			if (this.seaMoon.isAudit) {
				this.handleAudit()
			}
		},
		openSeaMoonDialog(isAudit) {
			this.$refs.reviwewForm.validate((valid) => {
				if (valid) {
					this.handleAudit()
				}
			})
		},
		tableRowClassName({ row, rowIndex }) {
			return row.level === this.rulesDialog.level ? 'high-light' : ''
		},
		goBack() {
			this.$emit('goBack')
		},
		// 一审二审弹窗
		openAuditDialog(status) {
			this.auditDialog.result = status
			this.auditDialog.visible = true
		},
		closeAuditDialog() {
			this.$refs.reviwewForm.resetFields()
		},
		handleAudit(form) {
			this.auditDialog.loading = true
			const params = {
				ids: [this.detailData.id],
				auditStatus: form.status,
				remark: form.remark
			}
			const loading = this.$loading({
				lock: true,
				text: 'Loading',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			})
			this.$api
				.proxyRebateAuditAPI(params)
				.then((res) => {
					loading.close()
					this.$message({
						type: 'success',
						message: this.$t('common.action_success')
					})
					this.visible = false
					this.auditDialog.loading = false
					this.$emit('goBack', true)
				})
				.catch(() => {
					loading.close()
					this.visible = false
					this.auditDialog.loading = false
				})
		}
	}
}
</script>

<style lang="scss" scoped>
.tree-box {
	width: 320px;
	flex-shrink: 0;
	height: auto;
	overflow: auto;
	::v-deep .el-card__body {
		padding-left: 0px;
		width: 100%;
	}
}
::v-deep .el-tabs__item {
	font-size: 16px;
	font-weight: bold;
}
.treeRight {
	width: calc(100% - 245px);
	display: flex;
	-o-display: flex;
	-ms-display: flex;
	-webkit-display: flex;
	-moz-display: flex;
	flex-direction: column;
	-o-flex-direction: column;
	-ms-flex-direction: column;
	-webkit-flex-direction: column;
	-moz-flex-direction: column;
	overflow-y: auto;
	overflow-x: hidden;
}
.txtfeed {
	width: 160px;
	table-layout: fixed;
	word-break: break-all;
	overflow: hidden;
}
.audit-wrapper {
	color: #666666;
	margin: 24px 16px 20px 16px;
	.head {
		height: 70px;
		line-height: 70px;
		width: 100%;
		background: #000;
		border-radius: 2px;
		padding: 0 30px;
		.title {
			font-weight: 600;
			color: rgb(192, 190, 190);
		}
		.right-btn {
			float: right;
		}
	}

	.audit-content {
		.block {
			padding: 10px 30px 0px 30px;
			.name {
				font-weight: 600;
				color: #303133;
			}
			.block-content {
				position: relative;
				width: 100%;
				margin-top: 15px;
			}
		}
		.rebate-details {
			padding-bottom: 20px;
			.details-page {
				margin-top: 10px;
				position: absolute;
				right: 0;
			}
		}
	}
}

.count-single-row {
	height: 40px;
	p {
		height: 40px;
		line-height: 40px;
		color: #5c5c5c;
		font-weight: 700;
		span {
			display: inline-block;
			width: 20px;
			height: 20px;
		}
	}
}

.audit-dialog {
	::v-deep .el-dialog__header {
		color: #909399;
		font-weight: 700;
		text-align: center;
	}
}

.active-dialog {
	::v-deep .el-dialog__header {
		padding: 10px 20px 10px;
	}
	::v-deep .el-dialog__body {
		padding: 20px 20px;
	}
	.tabs-wrapper {
		display: flex;
		.tab {
			width: 120px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			border: 1px solid rgba(0, 0, 0, 0.15);
			font-size: 14px;
			font-weight: 600;
			color: #000000a3;
			cursor: pointer;
			margin-right: 20px;
		}
	}
	.active {
		background: #00bfbf;
	}
	.type-name {
		padding: 0 0 20px;
		font-weight: 600;
		.num {
			color: blue;
		}
	}
}
::v-deep .el-table {
	.high-light {
		color: #0dc5e3;
	}
}
.detailBoxStyle {
	border: none !important;
	margin-top: 10px;
	overflow: hidden;
}
</style>
